package com.lichuang.fruitvarietysm.user.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lichuang.fruitvarietysm.user.entity.ShippingAddress;
import com.lichuang.fruitvarietysm.user.entity.User;
import com.lichuang.fruitvarietysm.user.finalproperties.AccountCode;
import com.lichuang.fruitvarietysm.user.finalproperties.DefaultAddress;
import com.lichuang.fruitvarietysm.user.finalproperties.Status;
import com.lichuang.fruitvarietysm.user.mapper.ShippingAddressMapper;
import com.lichuang.fruitvarietysm.user.service.impl.ShippingAddressServiceImpl;
import jdk.net.SocketFlow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 收货地址表 前端控制器
 * </p>
 *
 * @author shuaihang
 * @since 2019-05-27
 */
@RestController
@RequestMapping("//shipping-address")
public class ShippingAddressController {

    @Autowired
    private ShippingAddressMapper shippingAddressMapper;

    @Autowired
    private ShippingAddressServiceImpl addressService;

    @Autowired
    private UserController userController;

    /**
     * 新增收货地址
     * @param shippingAddress
     * @param isDefaultAddress
     * @param loginToken
     * @return
     */
    @RequestMapping("/addShippingAddress")
    public Map addShippingAddress(ShippingAddress shippingAddress, Boolean isDefaultAddress, @CookieValue(value = "loginToken") String loginToken) {
        Map<String, Object> map = new HashMap<>();
        User user = userController.getCurrentUser(loginToken);
        if (user == null) {
            map.put("code", AccountCode.UNLOGIN);
            map.put("message", "请先登录");
            return map;
        }
        shippingAddress.setUserId(user.getUserId())
                .setCreateTime(LocalDateTime.now());
        int insertNum = shippingAddressMapper.insert(shippingAddress);
        if (insertNum >= 1) {
            if (isDefaultAddress != null && isDefaultAddress) {
                shippingAddress.setIsDefault(DefaultAddress.ISDEFAULT.getValue());
            }
            map.put("code", AccountCode.OK);
            map.put("message", "添加地址成功");
            return map;
        } else {
            map.put("code", AccountCode.NO);
            map.put("message", "添加地址失败");
            return map;
        }

    }

    /**
     * 查询收货地址
     *
     * @param loginToken
     * @return
     */
    @RequestMapping("/selectShippingAddress")
    public List<ShippingAddress> selectShippingAddress(@CookieValue(value = "loginToken") String loginToken) {
        Map<String, Object> map = new HashMap<>();
        User user = userController.getCurrentUser(loginToken);
        if (user == null) {
            map.put("code", AccountCode.UNLOGIN);
            map.put("message", "请先登录");
            return null;
        }
        ShippingAddress shippingAddress = new ShippingAddress();
        shippingAddress.setUserId(user.getUserId()).setStatus(Status.NORMAL.getValue());
        QueryWrapper queryWrapper = new QueryWrapper(shippingAddress);
        List<ShippingAddress> shippingAddressList = shippingAddressMapper.selectList(queryWrapper);
        return shippingAddressList;
    }

    @RequestMapping("/selectShippingAddressById")
        public ShippingAddress selectShippingAddressById(Integer shippingId) {
        return shippingAddressMapper.selectById(shippingId);
    }

    @RequestMapping("/deleteShippingAddressById")
    public Integer deleteShippingAddressById(Integer shippingId) {
        ShippingAddress shippingAddress = new ShippingAddress();
        shippingAddress.setShippingId(shippingId);
        shippingAddress.setStatus(Status.LOCKED.getValue());
        return shippingAddressMapper.updateById(shippingAddress);
    }

    @RequestMapping("/getShippingAddressTextById")
    public String getShippingAddressTextById(Integer shipping_address_id){
        return addressService.getShippingAddressTextById(shipping_address_id);
    }

}
